package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.BlogSelectAdapter;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.bean.User;

/**田家选择农货，然后发布关于这个农货的种植日志
 * 田家的农货列表，在这里选择农货后，跳转到
 * 
 * @author GuiyouZhang
 * 2015年9月20日 下午10:15:47
 */
public class JournalListActivity extends BaseActivity implements OnClickListener {

    private TextView titleText;
    private ImageButton titleLeft;
    private Button titleRight;

	private LinearLayout llHasCrops;
	private XtomRefreshLoadmoreLayout layout;
	private XtomListView listView;
	private TextView tvRelease;
	private LinearLayout llHasNocrops;
	private TextView tvAddCrop;

    private int page;
    private ArrayList<Blog> blogs = new ArrayList<Blog>();
    private BlogSelectAdapter adapter;
    private User user;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_journal_list);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
		getCropsList();
    }
    
    private void getCropsList() {
    	if (user != null) {
    		getNetWorker().blogList("3", user.getId(), "1", "1", "", "1", "", "", "", "", "", "", "", "", page+"");
		}
    }
    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
        case BLOG_LIST:
            showProgressDialog("请稍候");
            break;
        default:
            break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
        case BLOG_LIST:
            cancelProgressDialog();
            break;
        default:
            break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
        case BLOG_LIST:
            String page = netTask.getParams().get("page");
            HemaPageArrayResult<Blog> bResult = (HemaPageArrayResult<Blog>) baseResult;
            if (bResult == null || bResult.getObjects() == null)
                return;
            ArrayList<Blog> goods = bResult.getObjects();
            if ("0".equals(page)) {// 刷新
                layout.refreshSuccess();
                layout.setRefreshable(false);
                blogs.clear();
                blogs.addAll(goods);
                if (blogs.size() == 0) {
                    layout.setLoadmoreable(false);
                	llHasCrops.setVisibility(View.GONE);
                	llHasNocrops.setVisibility(View.VISIBLE);
				}else {
                    layout.setLoadmoreable(true);
		        	llHasCrops.setVisibility(View.VISIBLE);
		        	llHasNocrops.setVisibility(View.GONE);
				}
            }else {//加载更多
                layout.loadmoreSuccess();
                if (goods.size()>0) {
                    blogs.addAll(goods);
                }else {
                    layout.setLoadmoreable(false);
                    XtomToastUtil.showShortToast(mContext, "已经到最后啦");
                }
            }
            refreshDatas(-1);
            break;
        default:
            break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
        case BLOG_LIST:
            layout.loadmoreFailed();
            showTextDialog(baseResult.getMsg());
            break;
        default:
            break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
        case BLOG_LIST:
            layout.loadmoreFailed();
            refreshDatas(failedType);
            break;
        default:
            break;
        }
    }
    
    private void refreshDatas(int failedType) {
        if (null == adapter) {
            adapter = new BlogSelectAdapter(mContext, blogs);
            adapter.setEmptyString("没有农货可以选择");
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        adapter.setFailtype(failedType);
    }

    @Override
    protected void findView() {
        titleText = (TextView) findViewById(R.id.title_text);
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleRight = (Button) findViewById(R.id.title_right_btn);

		llHasCrops = (LinearLayout)findViewById( R.id.layout_has_crops );
        layout = (XtomRefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
        listView = (XtomListView)findViewById( R.id.listview );
		tvRelease = (TextView)findViewById( R.id.tv_release );
		llHasNocrops = (LinearLayout)findViewById( R.id.layout_has_no_crops );
		tvAddCrop = (TextView)findViewById( R.id.tv_add_crop );
    }

    @Override
    protected void setListener() {
        titleText.setText("发布日志");
        titleLeft.setOnClickListener(this);
        titleRight.setVisibility(View.GONE);
        
        tvRelease.setOnClickListener(this);
        tvAddCrop.setOnClickListener(this);
        layout.setOnStartListener(new OnStartListener() {
			
			@Override
			public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
				page=0;
				getCropsList();
			}
			
			@Override
			public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
				page++;
				getCropsList();
			}
		});
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.title_left_btn:
            finish();
            break;
        case R.id.tv_release:
            Blog crop = null;
            for (Blog blog : blogs) {
                if (blog.isCheck())
                    crop = blog;
            }
        	if(crop == null) {
        		showTextDialog("请选择一个农产品!");
        		break;
        	}
            Intent itEdit = new Intent(mContext, ReleaseJournalActivity.class);
            itEdit.putExtra("blog", crop);
            startActivity(itEdit);
            finish();
            break;
        case R.id.tv_add_crop://去发布农作物
        	Intent itAdd = new Intent(mContext, ReleaseGoodsActivity.class);
            itAdd.putExtra("blog_id", "无");
            startActivity(itAdd);
        	break;
        default:
            break;
        }
    }

	@Override
	protected void getExras() {
		
	}
}
